Introduce gtk_tree_view_is_blank_at_pos()
authorKristian Rietveld <kris@gtk.org>
Fri, 7 Jan 2011 14:35:35 +0000 (15:35 +0100)
committerKristian Rietveld <kris@gtk.org>
Fri, 7 Jan 2011 14:38:06 +0000 (15:38 +0100)
This function is useful to figure out whether the tree view is "blank"
at a given location.  For such locations you might want to popup a
custom popup menu, clear the current selection or start rubber banding.
In the future, we are planning on updating GtkTreeView's user
interactions to take advantage of this new function.

Part of bug 350618.

gtk/gtk.symbols
gtk/gtktreeprivate.h
gtk/gtktreeview.c
gtk/gtktreeview.h
gtk/gtktreeviewcolumn.c

index 476bbeb06caaa73b37284ef98501358525315e83..919ddd4a54bde52ef6750366af8f8b889f65158b 100644 (file)
@@ -3350,6 +3350,7 @@ gtk_tree_view_grid_lines_get_type G_GNUC_CONST
 gtk_tree_view_insert_column
 gtk_tree_view_insert_column_with_attributes G_GNUC_NULL_TERMINATED
 gtk_tree_view_insert_column_with_data_func
+gtk_tree_view_is_blank_at_pos
 gtk_tree_view_is_rubber_banding_active
 gtk_tree_view_map_expanded_rows
 gtk_tree_view_move_column_after
index 9ee61f1ef907226654fc4b455cc05e7cefa2c319..a47a17b5b0687aa3d4e6f3f56512e1c86bce2954 100644 (file)
@@ -126,6 +126,11 @@ GtkCellRenderer  *_gtk_tree_view_column_get_cell_at_pos  (GtkTreeViewColumn  *co
                                                           GdkRectangle       *background_area,
                                                           gint                x,
                                                           gint                y);
+gboolean          _gtk_tree_view_column_is_blank_at_pos  (GtkTreeViewColumn  *column,
+                                                          GdkRectangle       *cell_area,
+                                                          GdkRectangle       *background_area,
+                                                          gint                x,
+                                                          gint                y);
 
 void             _gtk_tree_view_column_cell_render      (GtkTreeViewColumn  *tree_column,
                                                          cairo_t            *cr,
index fc599c996f6b2f8320efa463a0087df4b1d1bb53..76b748fc1c4fedf48cd05d89e6704ffa5b017de4 100644 (file)
@@ -14039,6 +14039,109 @@ gtk_tree_view_get_visible_range (GtkTreeView  *tree_view,
   return retval;
 }
 
+/**
+ * gtk_tree_view_is_blank_at_pos:
+ * @tree_view: A #GtkTreeView
+ * @x: The x position to be identified (relative to bin_window)
+ * @y: The y position to be identified (relative to bin_window)
+ * @path: (out) (allow-none): A pointer to a #GtkTreePath pointer to be filled in, or %NULL
+ * @column: (out) (allow-none): A pointer to a #GtkTreeViewColumn pointer to be filled in, or %NULL
+ * @cell_x: (out) (allow-none): A pointer where the X coordinate relative to the cell can be placed, or %NULL
+ * @cell_y: (out) (allow-none): A pointer where the Y coordinate relative to the cell can be placed, or %NULL
+ *
+ * Determine whether the point (@x, @y) in @tree_view is blank, that is no
+ * cell content nor an expander arrow is drawn at the location. If so, the
+ * location can be considered as the background. You might wish to take
+ * special action on clicks on the background, such as clearing a current
+ * selection, having a custom context menu or starting rubber banding.
+ *
+ * The @x and @y coordinate that are provided must be relative to bin_window
+ * coordinates.  That is, @x and @y must come from an event on @tree_view
+ * where <literal>event->window == gtk_tree_view_get_bin_window (<!-- -->)</literal>.
+ *
+ * For converting widget coordinates (eg. the ones you get from
+ * GtkWidget::query-tooltip), please see
+ * gtk_tree_view_convert_widget_to_bin_window_coords().
+ *
+ * The @path, @column, @cell_x and @cell_y arguments will be filled in
+ * likewise as for gtk_tree_view_get_path_at_pos().  Please see
+ * gtk_tree_view_get_path_at_pos() for more information.
+ *
+ * Return value: %TRUE if the area at the given coordinates is blank,
+ * %FALSE otherwise.
+ *
+ * Since: 3.0
+ */
+gboolean
+gtk_tree_view_is_blank_at_pos (GtkTreeView       *tree_view,
+                               gint                x,
+                               gint                y,
+                               GtkTreePath       **path,
+                               GtkTreeViewColumn **column,
+                               gint               *cell_x,
+                               gint               *cell_y)
+{
+  GtkRBTree *tree;
+  GtkRBNode *node;
+  GtkTreeIter iter;
+  GtkTreePath *real_path;
+  GtkTreeViewColumn *real_column;
+  GdkRectangle cell_area, background_area;
+
+  g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), FALSE);
+
+  if (!gtk_tree_view_get_path_at_pos (tree_view, x, y,
+                                      &real_path, &real_column,
+                                      cell_x, cell_y))
+    /* If there's no path here, it is blank */
+    return TRUE;
+
+  if (path)
+    *path = real_path;
+
+  if (column)
+    *column = real_column;
+
+  gtk_tree_model_get_iter (tree_view->priv->model, &iter, real_path);
+  _gtk_tree_view_find_node (tree_view, real_path, &tree, &node);
+
+  /* Check if there's an expander arrow at (x, y) */
+  if (real_column == tree_view->priv->expander_column
+      && gtk_tree_view_draw_expanders (tree_view))
+    {
+      gboolean over_arrow;
+
+      over_arrow = coords_are_over_arrow (tree_view, tree, node, x, y);
+
+      if (over_arrow)
+        {
+          if (!path)
+            gtk_tree_path_free (real_path);
+          return FALSE;
+        }
+    }
+
+  /* Otherwise, have the column see if there's a cell at (x, y) */
+  gtk_tree_view_column_cell_set_cell_data (real_column,
+                                           tree_view->priv->model,
+                                           &iter,
+                                           GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_PARENT),
+                                           node->children ? TRUE : FALSE);
+
+  gtk_tree_view_get_background_area (tree_view, real_path, real_column,
+                                     &background_area);
+  gtk_tree_view_get_cell_area (tree_view, real_path, real_column,
+                               &cell_area);
+
+  if (!path)
+    gtk_tree_path_free (real_path);
+
+  return _gtk_tree_view_column_is_blank_at_pos (real_column,
+                                                &cell_area,
+                                                &background_area,
+                                                x, y);
+}
+
 static void
 unset_reorderable (GtkTreeView *tree_view)
 {
index a828a40c1ce7403f083b7f0214fc7e23022a6c42..9e2227b026473b26934d57ba9caee9991c9b2344 100644 (file)
@@ -336,6 +336,13 @@ void                   gtk_tree_view_get_visible_rect              (GtkTreeView
 gboolean               gtk_tree_view_get_visible_range             (GtkTreeView               *tree_view,
                                                                    GtkTreePath              **start_path,
                                                                    GtkTreePath              **end_path);
+gboolean               gtk_tree_view_is_blank_at_pos               (GtkTreeView               *tree_view,
+                                                                    gint                       x,
+                                                                    gint                       y,
+                                                                    GtkTreePath              **path,
+                                                                    GtkTreeViewColumn        **column,
+                                                                    gint                      *cell_x,
+                                                                    gint                      *cell_y);
 
 /* Drag-and-Drop support */
 void                   gtk_tree_view_enable_model_drag_source      (GtkTreeView               *tree_view,
index 612732f5499f73b9f1e2c5a29d976ebfd301a568..eebcf5ecdcaf2ba4ca2501770669fdda4ee298f9 100644 (file)
@@ -1515,6 +1515,45 @@ _gtk_tree_view_column_get_cell_at_pos (GtkTreeViewColumn *column,
   return match;
 }
 
+gboolean
+_gtk_tree_view_column_is_blank_at_pos (GtkTreeViewColumn *column,
+                                       GdkRectangle      *cell_area,
+                                       GdkRectangle      *background_area,
+                                       gint               x,
+                                       gint               y)
+{
+  GtkCellRenderer *match;
+  GdkRectangle cell_alloc, aligned_area, inner_area;
+  GtkTreeViewColumnPrivate *priv = column->priv;
+
+  match = _gtk_tree_view_column_get_cell_at_pos (column,
+                                                 cell_area,
+                                                 background_area,
+                                                 x, y);
+  if (!match)
+    return FALSE;
+
+  gtk_cell_area_get_cell_allocation (priv->cell_area,
+                                     priv->cell_area_context,
+                                     priv->tree_view,
+                                     match,
+                                     cell_area,
+                                     &cell_alloc);
+
+  gtk_cell_area_inner_cell_area (priv->cell_area, priv->tree_view,
+                                 &cell_alloc, &inner_area);
+  gtk_cell_renderer_get_aligned_area (match, priv->tree_view, 0,
+                                      &inner_area, &aligned_area);
+
+  if (x < aligned_area.x ||
+      x > aligned_area.x + aligned_area.width ||
+      y < aligned_area.y ||
+      y > aligned_area.y + aligned_area.height)
+    return TRUE;
+
+  return FALSE;
+}
+
 /* Public Functions */